package leetcode;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
 * ClassName: ShuenfT1
 * Package: leetcode
 * Description:
 *
 * @Author wjn
 * @Create 2025/9/7 09:57
 * @Version 1.0
 */
public class ShuenfT1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();
        while (T-- > 0) {
            int n = scanner.nextInt(); // 用户数量
            int[] morning = new int[n];
            Map<Integer, Integer> posMap = new HashMap<>();

            for (int i = 0; i < n; i++) {
                morning[i] = scanner.nextInt();
                posMap.put(morning[i], i);
            }
            int[] evening = new int[n];
            for (int i = 0; i < n; i++) {
                evening[i] = scanner.nextInt();
            }
            int maxLen = 0;
            int prevPos = -1;
            for (int user : evening) {
                int currPos = posMap.get(user);
                if (currPos > prevPos) {
                    maxLen++;
                    prevPos = currPos;
                }
            }
            System.out.println(n - maxLen);
        }
        scanner.close();
    }
}
